
(function () {
    var updateTimeout = 15000;
    var i = 0;
    var navWindow;
    var aps='';
	if(Ti.Platform.osname== 'android') {
  		 aps='../';
    }
    var mainWindow = Ti.UI.createWindow({
       // backgroundImage: Codestrong.ui.mainBackgroundImage,
        title: 'Dashboard',
        navBarHidden: true,
        exitOnClose: true,
        backgroundColor:'White',
        //backgroundImage:aps+'images/main_im.png',
    });
    var viewFade = Ti.UI.createView({
        backgroundColor: '#fff',
        borderColor: '#888',
        borderWidth: 4,
        height: Codestrong.ui.dashboardHeight,
        width: Codestrong.ui.dashboardWidth,
        bottom: 20,
        opacity: 0.75,
        borderRadius: 8
    });
    var viewIcons = Ti.UI.createView({
        height: Codestrong.ui.dashboardHeight,
        width: Codestrong.ui.dashboardWidth,
        bottom: 20,
        borderRadius: 0,
        layout: 'horizontal'
    });
   // mainWindow.add(viewFade);
   // mainWindow.add(viewIcons);

    // handle cross-platform navigation
    if (Codestrong.isAndroid()) {
    	
        Codestrong.navGroup = {
            open: function (win, obj) {
                win.open(obj);
            },
            close: function (win, obj) {
                win.close(obj);
             
            }
        };
        navWindow = mainWindow;
    } else {
        navWindow = Ti.UI.createWindow();
        Codestrong.navGroup = Ti.UI.iPhone.createNavigationGroup({
            window: mainWindow
        });
        navWindow.add(Codestrong.navGroup);
    }


	var createButton = function (icon) {
		 var iconWin = undefined;
		 var vi = Ti.UI.createView({
           // backgroundImage: icon.image,
        	//image:   icon.image,
        	backgroundImage:   icon.image,
            height: icon.height,
            width: icon.width,
            bottom:icon.bottom,
            touchEnabled:true,
         
			borderWidth:0,
			backgroundColor:'transparent',
			//opacity:0.1,
			index:icon.index,
			
        });
        var bt = Titanium.UI.createButton({
			 /*
			 title:Buttons[i].title,
			 font:{
				fontSize:27,
				fontWeight:'bold',
			 },
			 color:Buttons[i].color,
			 */
			 backgroundImage:   icon.image,
			 backgroundSelectedImage:icon.image_,
			 height: icon.height,
             width: icon.width,
		    
		}); 
		/*
        bt.addEventListener('touchstart', function(e)
		{
		   var k=e.source.index;
		   e.source.backgroundImage=icon.image_;
		 
		});
		 
		bt.addEventListener("touchend", function(e)
		{
		   var k=e.source.index;
		   e.source.backgroundImage=icon.image;
		 
		});
		bt.addEventListener("touchcancel", function(e)
		{
			var k=e.source.index;
			e.source.backgroundImage=icon.image;
			
		});
		*/
        bt.addEventListener('click', function (e) {
        
		   
        	iconWin = icon.func(icon.args);
           // iconWin.orientationModes = [Ti.UI.PORTRAIT];

            // add a left navigation button for ios
            if (!Codestrong.isAndroid()) {
                var leftButton = Ti.UI.createButton({
                    backgroundImage: 'images/6dots.png',
                    width: 41,
                    height: 30
                });
                leftButton.addEventListener('click', function () {
                    Codestrong.navGroup.close(iconWin, {
                        animated: true
                    });
                       
                });
                iconWin.leftNavButton = leftButton;
            }
        	if (Codestrong.isAndroid()) {
		    	 iconWin.navBarHidden = true;
			} 
			else
			{
			  	  iconWin.navBarHidden = true;
			}

           
            Codestrong.navGroup.open(iconWin, {
                animated: true
            });
            var k=e.source.index;
		    e.source.backgroundImage=icon.image;
        });
        var text = Titanium.UI.createLabel({
			text:icon.title,
			font:{
					fontSize:38,
					fontWeight:'bold',
				 },
			//color:Buttons.color,
			right:100,
			width:'auto',
			height:'auto',
			touchEnabled:false,
		}); 
        vi.add(bt)
		//vi.add(text)
        return vi;
	}
    

	// Layout the dashboard icons
    for (i = 0; i < Codestrong.ui.icons.list.length; i++){
        //viewIcons.add(createIcon(Codestrong.ui.icons.list[i]));
       
        mainWindow.add(createButton(Codestrong.ui.icons.list[i]));
    }
 
   
    
    
 
 
 

    var vi_top = Titanium.UI.createView({
		 width:Titanium.Platform.displayCaps.platformWidth,
		 top:0,
		 height:50,
		 backgroundImage:aps+'images/main_logo.png',
   });
   /*
   var vi_top_image = Titanium.UI.createImageView({
		image:aps+'images/logothaytro.png',
		left:0,
		width:200,
   });
   vi_top.add(vi_top_image);
   */
   var myTxtVariable ='Trường trực tuyến thaytro.vn Nâng bước anh tài; Hướng tới tương lai.';
   var marquee = Ti.UI.createWebView({
	    	width:Titanium.Platform.displayCaps.platformWidth,
	        height : 50,
	        left : 0,
	        html:"<marquee WIDTH=600 HEIGHT=50 behavior='scroll' direction='left' ><font color='#00508d' size=4>"+myTxtVariable+"</font></marquee>",
	        backgroundColor : 'transparent',
	        touchEnable:false
	    });
   vi_top.add(marquee);


   mainWindow.add(vi_top);
   
   var viewimage_top_bottom = Titanium.UI.createView({
		 width:Titanium.Platform.displayCaps.platformWidth,
		 top:50,
		 bottom:500,
		 backgroundColor:'red',//'transparent',
   });
   mainWindow.add(viewimage_top_bottom);
   
   var viewimage_top = Titanium.UI.createScrollableView({
		 width:Titanium.Platform.displayCaps.platformWidth,
		 top:0,
		 bottom:65,
		// showPagingControl:true,
		 
	});

	viewimage_top.addEventListener('scrollEnd', function(e){
		//Titanium.API.info(e.currentPage)
		
		for (i = 0; i < viewimage_bottom.children.length; i++) {
			var v1 =viewimage_bottom.children[i];
			var lb=v1.children[0]
			if(i==e.currentPage){
				lb.color='white'
				v1.backgroundImage=dataimage[i].Image2;
			}
				
			else{
				lb.color='#00508d'
				v1.backgroundImage=dataimage[i].Image1;
			}
			
			
		}
			
			
	});
    viewimage_top_bottom.add(viewimage_top);
  
    var viewimage_bottom = Titanium.UI.createView({
		 width:Titanium.Platform.displayCaps.platformWidth,
		 height:65,
		 bottom:0,
		 backgroundColor:'transparent',
	});
	viewimage_top_bottom.add(viewimage_bottom);
    
   ///Add 5 image to view image
    var dataimage=[
    	{Id:1,Text_:'Trải nghiệm',Image1:aps+'images/main_im1.png',Image2:aps+'images/main_im1_.png',Image3:aps+'images/main_im1__.png',IsSelect:1},
    	{Id:2,Text_:'Tiện ích',Image1:aps+'images/main_im1.png',Image2:aps+'images/main_im1_.png',Image3:aps+'images/main_im2__.png'},
    	{Id:3,Text_:'Cộng đồng',Image1:aps+'images/main_im1.png',Image2:aps+'images/main_im1_.png',Image3:aps+'images/main_im3__.png'},
    	{Id:4,Text_:'Tư vấn',Image1:aps+'images/main_im1.png',Image2:aps+'images/main_im1_.png',Image3:aps+'images/main_im4__.png'},
    	{Id:5,Text_:'Dịch vụ',Image1:aps+'images/main_im1.png',Image2:aps+'images/main_im1_.png',Image3:aps+'images/main_im1__.png'},
	
	]
   //Add view to ScroolView
   
   //Add Button in bottom
   var left=0;
   for (i = 0; i < dataimage.length; i++) {
   		var vi_Scrool = Titanium.UI.createView({
   			 backgroundImage:dataimage[i].Image3,
			 index:i,
			 
		});
		
   		viewimage_top.addView(vi_Scrool);
   		
   	
   	
   	
   		 var viewimagedetail = Titanium.UI.createView({
			 width:Titanium.Platform.displayCaps.platformWidth/dataimage.length,
			 left:left,
			 height:65,
			 bottom:0,
			 backgroundImage:dataimage[i].Image1,
			 backgroundColor:'transparent',
			 index:i
		});
		
		var lb = Titanium.UI.createLabel({
		    width:Titanium.Platform.displayCaps.platformWidth/dataimage.length,
			text:dataimage[i].Text_,
			textAlign:'center',
			font:{
				fontSize:22,//Ti.App.Properties.getString('fontsizeLarge'),
				fontWeight:'bold'
			},
			color:'#00508d',//'#652582',
			touchEnable:false,
			index:i
			
		});
		lb.addEventListener("click", function(e)
		{
			var k=e.source.index;
			viewimage_top.scrollToView(k)
			
		});
		viewimagedetail.add(lb);
		
		//alert(dataimage[i].IsSelect)
		if(dataimage[i].IsSelect==1)
		{
			viewimagedetail.backgroundImage=dataimage[i].Image2;
			viewimage_top.backgroundImage=dataimage[i].Image3;
			lb.color='white'
		}	
		else{
			viewimagedetail.backgroundImage=dataimage[i].Image1;
		}
				
		
		
		viewimagedetail.addEventListener("click", function(e)
		{
			var k=e.source.index;
			viewimage_top.scrollToView(k)

			
		});
		viewimage_bottom.add(viewimagedetail);
		left+=viewimagedetail.width
   }
   
   
    //////////////LOAD//////////////
    /////////Auto scroll///////
    /*
    var ar = viewimage_top.getViews();
	var t = 0;
	setInterval(function(e) {
	    if(t >= ar.length) {
	        t = 0;
	    }
	    viewimage_top.scrollToView(t);
	    t++;
	 
	}, 3000);
	*/
    //////////////LOAD//////////////
   
    if (Codestrong.isAndroid()) {
        mainWindow.open({
            animated: true
        });
    } else {
        navWindow.open({
            transition: Ti.UI.iPhone.AnimationStyle.CURL_DOWN
        });
    }

    
})();